$(function () {

	var custom = {
	
		stripesScrollSpeed: 20,
	
		init: function () {
		
			//hook up click event to submit button
			$('.subscribe button.submit').click(function () {
				
				//clear all errors
				$('.subscribe input[type=text]').removeClass('error');

				//Just a simple validate just to make sure fields are all filled					
				//It basically loop through all textfield, if it's empty, add error class
				$('.subscribe input[type=text]').each(function () {
					
					if ($(this).val() == '') {
						$(this).addClass('error');
						
					//just to validate the email
					} else if ($(this).data('type') == 'email' && !custom.validateEmail($(this).val())) {
						$(this).addClass('error');
					}
					
				});

				//if no error classes were found
				if ($('.subscribe .error').length == 0) {					
				
					//call submit function
				    $('.subscribe button.submit').hide();
				    $('#loading').show();
				    custom.submit();			
				}
				
				return false;			
			});
			
		},
		
		//ajax call for form submission
		submit: function () {
			
            

			//format form data into a long string
		    var formData = {
		        Email: document.getElementById('email').value,
		        FullName: document.getElementById('name').value
		    };
			var url = 'Subscribe/AddSubscribe?Name='+formData.FullName+"&Email="+formData.Email;
			//submit it to backend
			$.ajax({
				type: "post",
				url: url, /* you will need to change this*/
				beforeSend: function () {
					custom.scrollStripes();		
				},
				success: function(data){
			
				    if (data[0] == 'T') {
				        //if browser support CSS3, use CSS3, otherwise just hide and show			
				        //check for csstransforms36 generated by modernizr	   
				        if ($('html').hasClass('csstransforms3d')) {

				            $('.subscribe').addClass('flip');
				            $('.subscribe .subscribe-wrapper').addClass('flipIt');

				        } else {

				            $('.subscribe-form').fadeOut();
				            $('.subscribe-success').fadeIn();
				        }
				        $('.subscribe-success .subscribe-space').html("<h4>Thanks!</h4><p>We can't wait to show you our new design.<br/><br/><br/><button class=\"submit\" onclick=\"flipAgain()\">Add Aonther Subscriber!</button></p>");
				    }
				    else {
				        //if browser support CSS3, use CSS3, otherwise just hide and show			
				        //check for csstransforms36 generated by modernizr	   
				        if ($('html').hasClass('csstransforms3d')) {

				            $('.subscribe').addClass('flip');
				            $('.subscribe .subscribe-wrapper').addClass('flipIt');

				        } else {

				            $('.subscribe-form').fadeOut();
				            $('.subscribe-success').fadeIn();
				            
				        }
				        $('.subscribe-success .subscribe-space').html("<h4>Sorry!</h4><p>This Email Already is being used!</p><br/><br/><br/><button class=\"submit\" onclick=\"flipAgain()\">Use Another Email!</button>");
				    }
				}
			});
		},
		
		//function to animate the stripes
		scrollStripes: function () {
		
	    var current = 0;
	 
	    //Calls the scrolling function repeatedly
	    var init = setInterval(function () {
		    
        // 1 pixel row at a time
        current -= 1;
    
        // move the background with backgrond-position css properties
        $('.subscribe .stripes').css("backgroundPosition", current+"px 0");			    
		    
	    }, custom.stripesScrollSpeed);  			
		
		},
		
		//simplest email validation, need to revalidate it via backend
		validateEmail: function (email) {
	    var re = /\S+@\S+\.\S+/;
	    return re.test(email);		
		}
	
	}

	//run the script!
	custom.init();

});